package com.ayf.payment.game.api.controller;

import com.ayf.payment.game.api.dto.ChannelStatisticsDTO;
import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.dto.WxConfigDTO;
import com.ayf.payment.game.api.dto.platform.WxGatewayAddressDTO;
import com.ayf.payment.game.api.entity.MerchantConfig;
import com.ayf.payment.game.api.entity.platform.WxConfig;
import com.ayf.payment.game.api.entity.platform.WxGatewayAddress;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.service.MerchantConfigService;
import com.ayf.payment.game.api.service.platform.WxConfigService;
import com.ayf.payment.game.api.service.platform.WxGatewayAddressService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.wx.WxGatewayConfig;
import com.ayf.payment.game.api.wx.WxHttpUtil;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author wwl
 * @description:
 * @date 2020/8/5 11:29
 */
@RestController
@RequestMapping("/v/wx")
public class WxManagerController {

    @Autowired
    private WxConfigService wxConfigService;

    @Resource
    private WxGatewayConfig wxGatewayConfig;

    @Autowired
    private WxGatewayAddressService wxGatewayAddressService;

    @Autowired
    private MerchantConfigService merchantConfigService;


    /**
     * 创建公众号信息
     *
     * @param merchantDTO
     * @param configDTO
     * @return
     */
    @PostMapping("/updatewx")
    public CommonResult updatewx(MerchantDTO merchantDTO, @RequestBody WxConfigDTO configDTO) {
        if (configDTO == null) {
            throw new ParameterException("参数不能为空");
        }
        WxConfig originWxConfig = wxConfigService.getWxConfigByWxOriginId(configDTO.getWxOriginId());
        if (originWxConfig != null){
            if (originWxConfig.getMerchantId() == null){
                return new CommonResult<>(CommonConstants.RETURN_ERROR,"操作失败，公众号原始ID已被平台密保配置再使用");
            } else {
                if (!merchantDTO.getId().equals(originWxConfig.getMerchantId())){
                    return new CommonResult<>(CommonConstants.RETURN_ERROR,String.format("操作失败，公众号原始ID已被商户号[%s]再使用",originWxConfig.getMerchantId()));
                }
            }
        }
        WxConfig config = BeanConvertUtils.convert(configDTO,WxConfig.class);
        config.setMerchantId(merchantDTO.getId());
        config.setLastUpdateTime(new Date());
        config.setStatus(1);
        if (null == config.getId()) {
            config.setCreateTime(new Date());
            wxConfigService.insert(config);
        } else {
            wxConfigService.updateConfig(config);
        }
        MerchantConfig merchantConfig = new MerchantConfig();
        merchantConfig.setMerchantId(merchantDTO.getId());
        merchantConfig.setWechatMbType(configDTO.getWechatMbType());
        merchantConfigService.updateMessageBoxStatus(merchantConfig);
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, CommonConstants.OPERATION_SUCCESS, "修改成功");
    }

    /**
     * 获取商户自己的公众号
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/getWxConfig")
    public CommonResult<WxConfigDTO> getWxConfig(MerchantDTO merchantDTO) {

        WxConfigDTO wxConfig = wxConfigService.selectByMId(merchantDTO.getId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "", wxConfig);
    }

    /**
     * 微信菜单初始化
     *
     * @return
     */
    @GetMapping("/setWxMenu3")
    public CommonResult<List<ChannelStatisticsDTO>> setWxMenu(MerchantDTO merchantDTO) {
        WxConfigDTO config = wxConfigService.selectByMId(merchantDTO.getId());
        if (wxGatewayConfig.setMenu2(BeanConvertUtils.convert(config,WxConfig.class), merchantDTO.getId())) {
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS, "微信菜单初始化成功");
        }
        return new CommonResult<>(CommonConstants.RETURN_ERROR, "微信菜单初始化失败");
    }
    @PostMapping("/wxIpCheck")
    public CommonResult<List<WxGatewayAddressDTO>> wxIpCheck(MerchantDTO merchantDTO, @RequestBody WxGatewayAddressDTO wxGatewayAddressDTO){
        WxGatewayAddress wxGatewayAddress= wxGatewayAddressService.getWxGatewayAddressById(wxGatewayAddressDTO.getId());
        String url= WxGatewayConfig.WX_GATEWAY_PATH + wxGatewayAddress.getUserIp()+":"+wxGatewayAddress.getUserPort() + WxGatewayConfig.WX_GATEWAY_URL;
            try {
                CommonResult commonResult = WxHttpUtil.testWsdlConnection(url);
                if(commonResult.getCode()==200){
                    wxGatewayAddress.setStatus(1);
                    wxGatewayAddressService.update(wxGatewayAddress);
                    return commonResult;
                }
                wxGatewayAddress.setStatus(-1);
            } catch (Exception e) {
                wxGatewayAddress.setStatus(-1);
                return new CommonResult(CommonConstants.RETURN_ERROR,"检测IP不通！");
            }
         wxGatewayAddressService.update(wxGatewayAddress);
        return new CommonResult(CommonConstants.RETURN_ERROR,"检测IP不通！");
    }

    @PostMapping("/getWxIpList")
    public CommonResult<List<WxGatewayAddressDTO>> getWxGatewayAddressList(MerchantDTO merchantDTO){
        WxConfigDTO wxConfig=wxConfigService.selectByMId(merchantDTO.getId());
        List<WxGatewayAddressDTO> list= wxGatewayAddressService.selectByWxOriginId(wxConfig.getWxOriginId(),merchantDTO.getId());
        return new CommonResult<>(CommonConstants.RETURN_SUCCESS,"", list);
    }

    @PostMapping("/wxIpDel")
    public CommonResult wxIpDel(MerchantDTO merchantDTO, @RequestBody WxGatewayAddressDTO wxGatewayAddressDTO){

        boolean bool=wxGatewayAddressService.del(wxGatewayAddressDTO.getId());
        if(bool){
            return new CommonResult<>(CommonConstants.RETURN_SUCCESS,"删除成功", "");
        }else{
            return new CommonResult<>(CommonConstants.RETURN_ERROR,"删除失败", "");
        }
    }

}
